//
//  ViewController.m
//  qq好友
//
//  Created by song on 15/1/15.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "JOFriendGroup.h"
#import "JOFriend.h"
#import "JOTableHeader.h"
#import "JOFriendCell.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    JOFriendGroup *group = self.groups[section];
    int count = (group.isOpened ? group.friends.count :0);
    NSLog(@"count is %d",count);
    return   (group.isOpened ? group.friends.count :0);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    JOFriendCell *cell =[JOFriendCell cellWithTableView:tableView];
    cell.friend =[self.groups[indexPath.section] friends][indexPath.row];

   return cell;
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}


-(NSArray *)groups
{
    
    if(_groups==nil)
    {
        NSMutableArray *garray =[NSMutableArray array];
        NSArray *farray =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil]];
        for(NSDictionary *dict  in farray)
        {
            JOFriendGroup *group = [JOFriendGroup groupWithDict:dict];
            [garray addObject:group];
        }
        
        _groups =garray;
    }

    return _groups;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    JOTableHeader *header =[JOTableHeader headerViewWithTableView:tableView];
    header.group = self.groups[section];
    header.delegate = self;
    return header;
}

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

-(void)headViewClickName:(JOTableHeader *)header
{
    NSLog(@"tableview is reload");
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
 
    }

@end
